package com.hs;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;


public class GetAliYunPan {
    private static String strs = "QWERTYUIOPLKJHGFDSAZXCVBNM1234567890";
    private final static int MAXNUM = 100000;
    private static String[] strArry = null;
    private static int alength = 0;
    private static List<MBean> retList = new CopyOnWriteArrayList<MBean>();
    private static final ThreadLocal<HttpRequest> httpRequestThreadLocal = new ThreadLocal<>();
    static {
        strArry = strs.split("");
        alength = strArry.length;
    }

    public static void main(String[] args) throws InterruptedException {

        ExecutorService executorService = Executors.newFixedThreadPool(10);
        CountDownLatch downLatch = new CountDownLatch(MAXNUM);

        for (int i = 0; i < MAXNUM; i++) {
            executorService.execute(() -> {
                if(httpRequestThreadLocal.get() == null){
                    String url = "https://pan.teambition.com/pan/api/orgs/activate";
                    HttpRequest httpRequest = HttpUtil.createPost(url);

                    //cookie放这里
                    httpRequest.cookie("cookie放这里");
                    httpRequestThreadLocal.set(httpRequest);
                }
                final HttpRequest httpRequest = httpRequestThreadLocal.get();


                String activeCode = generateActiveCode();
                //需要改
                String body = "{\"code\":\""+ activeCode + "\",\"orgId\":\"这里需更改\"}";
                String contentTyple="application/json";
                httpRequest.body(body,contentTyple);
                HttpResponse httpResponse = httpRequest.execute();
                String retJson = httpResponse.body();
                if(retJson.indexOf("Rate") > -1){
                    System.out.println(retJson);
                    try {
                        TimeUnit.SECONDS.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }finally {
                        downLatch.countDown();
                    }
                    return;
                }

                JSONObject jsonObject = JSONUtil.parseObj(retJson);
                boolean success = (boolean) jsonObject.get("success");
                MBean mBean = new MBean().setActiveCode(activeCode)
                        .setRetStatus(success)
                        .setRetBody(retJson);
                retList.add(mBean);
                if(success){
                    System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + mBean.toString());
                    FileOutputStream foss = null;
                    try {
                        //观察success.txt文件有没有输出即可证明成功没有
                        foss = new FileOutputStream("./success.txt", false);
                        foss.write(mBean.toString().getBytes());
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        try {
                            foss.flush();
                            foss.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }

                }
                System.out.println(mBean.toString());
                downLatch.countDown();
            });
        }

        downLatch.await();
        //String listStr = retList.toString();
        //FileOutputStream fos = new FileOutputStream("./resp.txt");
        //fos.write(listStr.getBytes());
        executorService.shutdown();
        //fos.flush();
        //fos.close();


    }


    public static String generateActiveCode(){
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 1; i <= 6; i++) {
            int rd = random.nextInt(alength);
            sb.append(strArry[rd]);
        }
        return sb.toString();
    }
}
